package com.pengtu.gsj.service.impl.check.lzgl.roadProtect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lzgl.InPorMatDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.InPorMat;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.roadProtect.RoadProSupService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class RoadProSupServiceImpl implements RoadProSupService {

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public InPorMatDao inPorMatDao;
	
	@Override
	public Map<String, Object> getRoadProSups(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<InPorMat> inPorMats = new ArrayList<InPorMat>();
		List<Double> scoLists = new ArrayList<Double>();
		InPorMat inPorMat = new InPorMat();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String itemIds = (String)jsonObject.get("itemProjectId");
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double total = 0;
		double tableFull = yearFull * 4;
		Boolean flag = false;
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		map.put("att", list);
		if (StringUtils.isNotEmpty(itemIds)) {
			map.put("flag", true);
			//根据检索条件检索出符合条件的数据
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = itemIds.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				//根据tag检索是否有附件
				if( i > 3) {
					break;
				}
			double tempSco = 0;
			inPorMat = inPorMatDao.get(idArrs[i]);
			inPorMats.add(inPorMat);
			//1、根据相关附件算分，如果没有相对应的附件，就进行相应地扣分
			for (Attachment attachment : inPorMat.getAttachments()) {
				if ("slgcjg_xgjl".equals(attachment.getTag()) && flag.equals(false)) {
					flag = true;
				}
			}
			tempSco += flag.equals(true) ? yearFull/3 : 0;
			//2、发现问题如果及时整改，可得分yearFull/3，否则这项不得分
			tempSco += "1".equals(inPorMat.getIfTimeRect()) ? yearFull/3 : 0;
			//3、默认是执行了依法监督
			tempSco += yearFull/3;
			scoLists.add(tempSco);
			total += tempSco;
			}
		}
		//根据总得分算出完善度
		double percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("scoLists", scoLists);
		map.put("percent", percentage);
		map.put("inPorMats", inPorMats);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}


	/**
	 * 适用：获得涉路工程监督的监督记录
	 */
	@Override
	public List<InPorMat> getInPorMat(QueryFilter queryFilter) {
		return inPorMatDao.find(queryFilter);
	}
	
	
	/**
	 * 根据startTime, endTime和inPorMat（涉路工程）检索数据文件列表
	 */
	@Override
	public List<InPorMat> getCheckData(String startTime, String endTime, String proSupContent) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("year", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("year",  endTime, MatchType.LE);
		}
		queryFilter.addFilter("inPorMat", proSupContent, MatchType.EQ);
		return inPorMatDao.find(queryFilter);
	}
}
